package com.gitee.ricesugar.controller;

import com.gitee.ricesugar.common.Result;
import com.gitee.ricesugar.controller.request.BookPageRequest;
import com.gitee.ricesugar.entity.Book;
import com.gitee.ricesugar.service.IBookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/book")
public class BookController {

    @Autowired
    IBookService bookService;

    @PostMapping("/add")
    public Result add(@RequestBody Book obj) {
        bookService.add(obj);
        return Result.success();
    }

    @GetMapping("/page")
    public Result page(BookPageRequest obj) {
        return Result.success(bookService.page(obj));
    }

    @GetMapping("/record")
    public Result record(BookPageRequest obj) {
        return Result.success(bookService.record(obj));
    }

    @GetMapping("/{isbn}")
    public Result getByISBN(@PathVariable String isbn) {
        return Result.success(bookService.getByISBN(isbn));
    }

    @PutMapping("/borrow/{username}")
    public Result borrow(@PathVariable String username, @RequestBody List<Book> obj) {
        bookService.borrow(username, obj);
        return Result.success();
    }

    @PutMapping("/return")
    public Result returnBook(@RequestBody Integer id) {
        bookService.returnBook(id);
        return Result.success();
    }

    @PutMapping("/update")
    public Result update(@RequestBody Book obj) {
        bookService.update(obj);
        return Result.success();
    }

    @DeleteMapping("/delete/{isbn}")
    public Result deleteByISBN(@PathVariable String isbn) {
        bookService.deleteByISBN(isbn);
        return Result.success();
    }

    @DeleteMapping("/record/delete/{id}")
    public Result deleteRecordById(@PathVariable Integer id) {
        bookService.deleteRecordById(id);
        return Result.success();
    }
}
